<?php
class Bap extends MY_Controller {
	function Bap(){
		parent::MY_Controller();
		$this->validate_cookie();
	}
	
	function _remap(){
		//validate user first
		$URICount = $this->uri->total_segments();
		$usr = $this->uri->segment(2);	//get username
		$this->_validateUser($usr);
		
		//call function
		$func = $this->uri->segment(4);
		$params = array();
		for($i=5;$i<=$URICount; $i++){
			$params[] = $this->uri->segment($i);
		}
		
	    if ( method_exists($this, $func) ) {
	    	call_user_func_array( array($this, $func), $params);
	    }
	    else if (count($params)<=0){
			$this->index();
		}
	}
	
	function _translate_nav($nav, $userAlias){
		$nv = array();
		foreach ($nav as $title=>$value){
			$nv[$title] = array();
			foreach ($value as $item=>$val){
				$nv[$title][$item] = preg_replace("/(:user)/i",$userAlias, $val);
			}
		}
		return $nv;
	}
	
	function index(){
		$this->load->library('session');
		$usrAlias = md5($this->session->userdata('usrname'));
		
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');

		//layout first
		$this->load->model('departemen_model');
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array(
			'view'=>'template/horz_nav', 
			'view_data'=>array(
				'data'=>$usrAlias!=md5('admin')?$this->_translate_nav($CONF['nav']['users'],$usrAlias):$CONF['nav']['psdm']
			) 
		);
		$layoutConf['right_content'] = array('view'=>'bap/users/dashboard', 
			'view_data'=>array(
				'limit' => 5,//tampilkan sedikit saja$CONF['search']['limit'],
				'usrAlias'=>$usrAlias
			));
		$this->initialize_layout($layoutConf);
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript_default'][] = 'users/bap/dashboard';
		$pageConf['jscript'] = $pageConf['jscript_default'];
		$pageConf['css'] = $pageConf['css_default'];
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();		
	}
	
	function _search($type){ $this->{"__search_$type"}(); }
	
	function __search_SA(){
		$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($usrName);
		
		$this->load->config('rnd',true);
		$conf = $this->config->item('rnd');
		
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		$where = $this->input->post('where');
		
		$table_content = "";
		$row = 0;
		
		//layout data
		$this->load->model(array('perbuatan_model','perbuatan_detil_model',
			'bap_model','detachievement_model','bap_notifikasi_model'));
		
		$data = array();
		$dataCount = 0;
		$BAP = $this->bap_model->getActiveBAP(
			$usrName, false, $where, $limit, $offset, 
			$orderby? $orderby : array('bap_status_change'=>'desc'));
		foreach ($BAP->result_array() as $row){
			$data[$dataCount] = array('bap_id'=>$row['bap_id']);
			
			$PERB = (array)$this->perbuatan_model->select_by_id($row['perb_id'])->row();
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'perb_id'=> $PERB['perb_id'],
				'perb_tgl_lapor'=> date("Y-m-d H:i:s",strtotime($PERB['perb_tgl_lapor'])),
				'perb_tgl_kejadian'=> date("Y-m-d H:i:s",strtotime($PERB['perb_tgl_kejadian']))
			));
			
			$DEPERB = (array)$this->perbuatan_detil_model->select_by_id($PERB['perb_id'])->row();
			$DETACHIEV = (array)$this->detachievement_model->select_by_id($DEPERB['detachiev_id'])->row();
			
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'achiev_jenis'=> $DETACHIEV['achiev_jenis'],
				'achiev_bidang'=> $DETACHIEV['achiev_bidang'],
				'detachiev_tingkat'=> $DETACHIEV['detachiev_tingkat'],
				'detachiev_nama'=> $DETACHIEV['detachiev_nama']
			));
			
			//notification
			$NOTIF = $this->bap_notifikasi_model->getNotification($usrName, 'SA', $row['bap_id']);
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'bap_notif_opened'=> $this->bap_notifikasi_model->hasBeenOpened($usrName, 'SA', $row['bap_id']),
				'bap_notif_id'=> $NOTIF? $NOTIF['bap_notif_id'] : false   
			));
			
			$dataCount++;
		}
		
		$table_content = $this->load->view('bap/users/table_content_SA', array(
			'data'=>$data,
			'jenis_options'=>$conf['achievement']['jenis_options'],
			'tingkat_options'=>array_merge(
				$conf['detachievement']['tingkat_options']['PE'],
				$conf['detachievement']['tingkat_options']['PR']),
			'usrAlias'=>$usrAlias,
			'usrName'=>$usrName
			), true);
		echo json_encode(array(
			'table_content'=>$table_content,
			'table_num_record'=> $dataCount
		));
	}
	
	function __search_SI(){
		$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($usrName);
		
		$this->load->config('rnd',true);
		$conf = $this->config->item('rnd');
		
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		$where = $this->input->post('where');
		
		$table_content = "";
		$row = 0;
		
		//layout data
		$this->load->model(array('perbuatan_model','perbuatan_detil_model',
			'bap_model','detachievement_model','bap_notifikasi_model'));
		
		$data = array();
		$dataCount = 0;
		$BAP = $this->bap_model->getSignedBAP(
			$usrName, false, $where, $limit, $offset, 
			$orderby? $orderby : array('bap_status_change'=>'desc'));
		foreach ($BAP->result_array() as $row){
			$data[$dataCount] = array('bap_id'=>$row['bap_id']);
			
			$PERB = (array)$this->perbuatan_model->select_by_id($row['perb_id'])->row();
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'perb_id'=> $PERB['perb_id'],
				'perb_tgl_lapor'=> date("Y-m-d H:i:s",strtotime($PERB['perb_tgl_lapor'])),
				'perb_tgl_kejadian'=> date("Y-m-d H:i:s",strtotime($PERB['perb_tgl_kejadian']))
			));
			
			$DEPERB = (array)$this->perbuatan_detil_model->select_by_id($PERB['perb_id'])->row();
			$DETACHIEV = (array)$this->detachievement_model->select_by_id($DEPERB['detachiev_id'])->row();
			
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'achiev_jenis'=> $DETACHIEV['achiev_jenis'],
				'achiev_bidang'=> $DETACHIEV['achiev_bidang'],
				'detachiev_tingkat'=> $DETACHIEV['detachiev_tingkat'],
				'detachiev_nama'=> $DETACHIEV['detachiev_nama']
			));
			
			//notification
			$NOTIF = $this->bap_notifikasi_model->getNotification($usrName, 'SI', $row['bap_id']);
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'bap_notif_opened'=> $this->bap_notifikasi_model->hasBeenOpened($usrName, 'SI', $row['bap_id']),
				'bap_notif_id'=> $NOTIF? $NOTIF['bap_notif_id'] : false
			));
			
			$dataCount++;
		}
		
		$table_content = $this->load->view('bap/users/table_content_SI', array(
			'data'=>$data,
			'jenis_options'=>$conf['achievement']['jenis_options'],
			'tingkat_options'=>array_merge(
				$conf['detachievement']['tingkat_options']['PE'],
				$conf['detachievement']['tingkat_options']['PR']),
			'usrAlias'=>$usrAlias,
			'usrName'=>$usrName
			), true);
		echo json_encode(array(
			'table_content'=>$table_content,
			'table_num_record'=> $dataCount
		));
	}
	
	function __search_CO(){
		$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($usrName);
		
		$this->load->config('rnd',true);
		$conf = $this->config->item('rnd');
		
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		$where = $this->input->post('where');
		
		$table_content = "";
		$row = 0;
		
		//layout data
		$this->load->model(array('perbuatan_model','perbuatan_detil_model',
			'bap_model','detachievement_model','bap_notifikasi_model'));
		
		$data = array();
		$dataCount = 0;
		$BAP = $this->bap_model->getCounterBAP(
			$usrName, false, $where, $limit, $offset, 
			$orderby? $orderby : array('bap_status_change'=>'desc'));
		foreach ($BAP->result_array() as $row){
			$data[$dataCount] = array('bap_id'=>$row['bap_id']);
			
			$PERB = (array)$this->perbuatan_model->select_by_id($row['perb_id'])->row();
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'perb_id'=> $PERB['perb_id'],
				'perb_tgl_lapor'=> date("Y-m-d H:i:s",strtotime($PERB['perb_tgl_lapor'])),
				'perb_tgl_kejadian'=> date("Y-m-d H:i:s",strtotime($PERB['perb_tgl_kejadian']))
			));
			
			$DEPERB = (array)$this->perbuatan_detil_model->select_by_id($PERB['perb_id'])->row();
			$DETACHIEV = (array)$this->detachievement_model->select_by_id($DEPERB['detachiev_id'])->row();
			
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'achiev_jenis'=> $DETACHIEV['achiev_jenis'],
				'achiev_bidang'=> $DETACHIEV['achiev_bidang'],
				'detachiev_tingkat'=> $DETACHIEV['detachiev_tingkat'],
				'detachiev_nama'=> $DETACHIEV['detachiev_nama']
			));
			
			//notification
			$NOTIF = $this->bap_notifikasi_model->getNotification($usrName, 'CO', $row['bap_id']);
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'bap_notif_opened'=> $this->bap_notifikasi_model->hasBeenOpened($usrName, 'CO', $row['bap_id']),
				'bap_notif_id'=> $NOTIF? $NOTIF['bap_notif_id'] : false
			));
			
			$dataCount++;
		}
		
		$table_content = $this->load->view('bap/users/table_content_CO', array(
			'data'=>$data,
			'jenis_options'=>$conf['achievement']['jenis_options'],
			'tingkat_options'=>array_merge(
				$conf['detachievement']['tingkat_options']['PE'],
				$conf['detachievement']['tingkat_options']['PR']),
			'usrAlias'=>$usrAlias,
			'usrName'=>$usrName
			), true);
		echo json_encode(array(
			'table_content'=>$table_content,
			'table_num_record'=> $dataCount
		));
	}
	
	function __search_CR(){
		$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($usrName);
		
		$this->load->config('rnd',true);
		$conf = $this->config->item('rnd');
		
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		$where = $this->input->post('where');
		
		$table_content = "";
		$row = 0;
		
		//layout data
		$this->load->model(array('perbuatan_model','perbuatan_detil_model',
			'bap_model','detachievement_model','bap_notifikasi_model'));
		
		$data = array();
		$dataCount = 0;
		$BAP = $this->bap_model->getCounterRejectedBAP(
			$usrName, false, $where, $limit, $offset, 
			$orderby? $orderby : array('bap_status_change'=>'desc'));
		foreach ($BAP->result_array() as $row){
			$data[$dataCount] = array('bap_id'=>$row['bap_id']);
			
			$PERB = (array)$this->perbuatan_model->select_by_id($row['perb_id'])->row();
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'perb_id'=> $PERB['perb_id'],
				'perb_tgl_lapor'=> date("Y-m-d H:i:s",strtotime($PERB['perb_tgl_lapor'])),
				'perb_tgl_kejadian'=> date("Y-m-d H:i:s",strtotime($PERB['perb_tgl_kejadian']))
			));
			
			$DEPERB = (array)$this->perbuatan_detil_model->select_by_id($PERB['perb_id'])->row();
			$DETACHIEV = (array)$this->detachievement_model->select_by_id($DEPERB['detachiev_id'])->row();
			
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'achiev_jenis'=> $DETACHIEV['achiev_jenis'],
				'achiev_bidang'=> $DETACHIEV['achiev_bidang'],
				'detachiev_tingkat'=> $DETACHIEV['detachiev_tingkat'],
				'detachiev_nama'=> $DETACHIEV['detachiev_nama']
			));
			
			//notification
			$NOTIF = $this->bap_notifikasi_model->getNotification($usrName, 'CR', $row['bap_id']);
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'bap_notif_opened'=> $this->bap_notifikasi_model->hasBeenOpened($usrName, 'CR', $row['bap_id']),
				'bap_notif_id'=> $NOTIF? $NOTIF['bap_notif_id'] : false
			));
			
			$dataCount++;
		}
		
		$table_content = $this->load->view('bap/users/table_content_CR', array(
			'data'=>$data,
			'jenis_options'=>$conf['achievement']['jenis_options'],
			'tingkat_options'=>array_merge(
				$conf['detachievement']['tingkat_options']['PE'],
				$conf['detachievement']['tingkat_options']['PR']),
			'usrAlias'=>$usrAlias,
			'usrName'=>$usrName
			), true);
		echo json_encode(array(
			'table_content'=>$table_content,
			'table_num_record'=> $dataCount
		));
	}
	
	function __search_CA(){
		$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($usrName);
		
		$this->load->config('rnd',true);
		$conf = $this->config->item('rnd');
		
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		$where = $this->input->post('where');
		
		$table_content = "";
		$row = 0;
		
		//layout data
		$this->load->model(array('perbuatan_model','perbuatan_detil_model',
			'bap_model','detachievement_model','bap_notifikasi_model'));
		
		$data = array();
		$dataCount = 0;
		$BAP = $this->bap_model->getCounterApprovedBAP(
			$usrName, false, $where, $limit, $offset, 
			$orderby? $orderby : array('bap_status_change'=>'desc'));
		foreach ($BAP->result_array() as $row){
			$data[$dataCount] = array('bap_id'=>$row['bap_id']);
			
			$PERB = (array)$this->perbuatan_model->select_by_id($row['perb_id'])->row();
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'perb_id'=> $PERB['perb_id'],
				'perb_tgl_lapor'=> date("Y-m-d H:i:s",strtotime($PERB['perb_tgl_lapor'])),
				'perb_tgl_kejadian'=> date("Y-m-d H:i:s",strtotime($PERB['perb_tgl_kejadian']))
			));
			
			$DEPERB = (array)$this->perbuatan_detil_model->select_by_id($PERB['perb_id'])->row();
			$DETACHIEV = (array)$this->detachievement_model->select_by_id($DEPERB['detachiev_id'])->row();
			
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'achiev_jenis'=> $DETACHIEV['achiev_jenis'],
				'achiev_bidang'=> $DETACHIEV['achiev_bidang'],
				'detachiev_tingkat'=> $DETACHIEV['detachiev_tingkat'],
				'detachiev_nama'=> $DETACHIEV['detachiev_nama']
			));
			
			//notification
			$NOTIF = $this->bap_notifikasi_model->getNotification($usrName, 'CA', $row['bap_id']);
			$data[$dataCount] = array_merge($data[$dataCount], array(
				'bap_notif_opened'=> $this->bap_notifikasi_model->hasBeenOpened($usrName, 'CA', $row['bap_id']),
				'bap_notif_id'=> $NOTIF? $NOTIF['bap_notif_id'] : false
			));
			
			$dataCount++;
		}
		
		$table_content = $this->load->view('bap/users/table_content_CA', array(
			'data'=>$data,
			'jenis_options'=>$conf['achievement']['jenis_options'],
			'tingkat_options'=>array_merge(
				$conf['detachievement']['tingkat_options']['PE'],
				$conf['detachievement']['tingkat_options']['PR']),
			'usrAlias'=>$usrAlias,
			'usrName'=>$usrName
			), true);
		echo json_encode(array(
			'table_content'=>$table_content,
			'table_num_record'=> $dataCount
		));
	}
	
	function detailView($type, $bapID){ $this->{"__detailView_$type"}($bapID); }
	
	function __detailView_SI($bapID){
		$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($usrName);
		
		//load the perbuatan data
		$this->load->model(array('bap_model','perbuatan_model','perbuatan_detil_model',
			'perbuatan_bukti_model','pelapor_model', 'karyawan_model', 'karyawan_history_jabatan_model'));
		$data = array();
		$bapData =  (array) $this->bap_model->select_by_id($bapID)->row();
		$perbData = (array) $this->perbuatan_model->select_by_id($bapData['perb_id'])->row();
		$buktiperbData = $this->perbuatan_bukti_model->select_by_perbuatan($perbData['perb_id']);
		$deperbData = $this->perbuatan_detil_model->select_by_perbuatan($perbData['perb_id']);
		$laporData = (array)$this->pelapor_model->select_by_id($perbData['lapor_id'])->row();
		
		//insert to variable
		$data['bapData'] = $bapData;
		$data['perbData'] = $perbData;
		$data['laporData'] = $laporData;
		foreach($deperbData->result_array() as $res){
			$data['deperbData'][] = $res;
		}
		foreach($buktiperbData->result_array() as $res){
			$data['buktiperbData'][] = $res;
		}
		
		//layout the view
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');

		//layout first
		$this->load->model('kota_model');
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array(
			'view'=>'template/horz_nav', 
			'view_data'=>array(
				'data'=>$this->_translate_nav($CONF['nav']['users'],$usrAlias),
			)
		);
		$layoutConf['right_content'] = array(
			'view'=>"bap/users/detailView_SI",
			'view_data'=>array_merge(array(
				'kota_options'=> $this->kota_model->select_all(array('kota_nama'=>'asc')),
				'jenis_options'=>$CONF['achievement']['jenis_options'],
				'tingkat_options'=>array_merge(
					$CONF['detachievement']['tingkat_options']['PE'],
					$CONF['detachievement']['tingkat_options']['PR']),
				'usrAlias'=>$usrAlias,
				'usrName'=>$usrName
			),$data)
		);
		$this->initialize_layout($layoutConf);
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript'] = array_merge($pageConf['jscript_default'],
			array("users/bap/detailView_SI",'datepicker','Swiff.Uploader',
				'Fx.ProgressBar', 'utils/searchDialog','FancyUpload',
				'mooEditable/MooEditable', 'mooEditable/MooEditable.UI.MenuList',
				'mooEditable/MooEditable.Extras',
				));
		
		$pageConf['css'] = array_merge($pageConf['css_default'], 
			array('datepicker','FancyUpload', 'mooEditable/MooEditable', 
				'MooEditable/MooEditable.Extras','MooEditable/MooEditable.SilkTheme'));
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();
	}
	
	function __detailView_CO($bapID){
			$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($usrName);
		
		//load the perbuatan data
		$this->load->model(array('bap_model','perbuatan_model','perbuatan_detil_model',
			'perbuatan_bukti_model','pelapor_model', 'karyawan_model', 'karyawan_history_jabatan_model'));
		$data = array();
		$bapData =  (array) $this->bap_model->select_by_id($bapID)->row();
		$perbData = (array) $this->perbuatan_model->select_by_id($bapData['perb_id'])->row();
		$buktiperbData = $this->perbuatan_bukti_model->select_by_perbuatan($perbData['perb_id']);
		$deperbData = $this->perbuatan_detil_model->select_by_perbuatan($perbData['perb_id']);
		$laporData = (array)$this->pelapor_model->select_by_id($perbData['lapor_id'])->row();
		
		//insert to variable
		$data['bapData'] = $bapData;
		$data['perbData'] = $perbData;
		$data['laporData'] = $laporData;
		foreach($deperbData->result_array() as $res){
			$data['deperbData'][] = $res;
		}
		foreach($buktiperbData->result_array() as $res){
			$data['buktiperbData'][] = $res;
		}
		
		//layout the view
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');

		//layout first
		$this->load->model('kota_model');
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array(
			'view'=>'template/horz_nav', 
			'view_data'=>array(
				'data'=>$this->_translate_nav($CONF['nav']['users'],$usrAlias),
			)
		);
		$layoutConf['right_content'] = array(
			'view'=>"bap/users/detailView_CO",
			'view_data'=>array_merge(array(
				'kota_options'=> $this->kota_model->select_all(array('kota_nama'=>'asc')),
				'jenis_options'=>$CONF['achievement']['jenis_options'],
				'tingkat_options'=>array_merge(
					$CONF['detachievement']['tingkat_options']['PE'],
					$CONF['detachievement']['tingkat_options']['PR']),
				'usrAlias'=>$usrAlias,
				'usrName'=>$usrName
			),$data)
		);
		$this->initialize_layout($layoutConf);
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript'] = array_merge($pageConf['jscript_default'],
			array("users/bap/detailView_CO",'datepicker','Swiff.Uploader',
				'Fx.ProgressBar', 'utils/searchDialog','FancyUpload',
				'mooEditable/MooEditable', 'mooEditable/MooEditable.UI.MenuList',
				'mooEditable/MooEditable.Extras',
				));
		
		$pageConf['css'] = array_merge($pageConf['css_default'], 
			array('datepicker','FancyUpload', 'mooEditable/MooEditable', 
				'MooEditable/MooEditable.Extras','MooEditable/MooEditable.SilkTheme'));
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();
	}
	
	function __detailView_CR($bapID){
		$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($usrName);
		
		//load the perbuatan data
		$this->load->model(array('bap_model','perbuatan_model','perbuatan_detil_model',
			'perbuatan_bukti_model','pelapor_model', 'karyawan_model', 'karyawan_history_jabatan_model'));
		$data = array();
		$bapData =  (array) $this->bap_model->select_by_id($bapID)->row();
		$perbData = (array) $this->perbuatan_model->select_by_id($bapData['perb_id'])->row();
		$buktiperbData = $this->perbuatan_bukti_model->select_by_perbuatan($perbData['perb_id']);
		$deperbData = $this->perbuatan_detil_model->select_by_perbuatan($perbData['perb_id']);
		$laporData = (array)$this->pelapor_model->select_by_id($perbData['lapor_id'])->row();
		
		//insert to variable
		$data['bapData'] = $bapData;
		$data['perbData'] = $perbData;
		$data['laporData'] = $laporData;
		foreach($deperbData->result_array() as $res){
			$data['deperbData'][] = $res;
		}
		foreach($buktiperbData->result_array() as $res){
			$data['buktiperbData'][] = $res;
		}
		
		//layout the view
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');

		//layout first
		$this->load->model('kota_model');
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array(
			'view'=>'template/horz_nav', 
			'view_data'=>array(
				'data'=>$this->_translate_nav($CONF['nav']['users'],$usrAlias),
			)
		);
		$layoutConf['right_content'] = array(
			'view'=>"bap/users/detailView_CR",
			'view_data'=>array_merge(array(
				'kota_options'=> $this->kota_model->select_all(array('kota_nama'=>'asc')),
				'jenis_options'=>$CONF['achievement']['jenis_options'],
				'tingkat_options'=>array_merge(
					$CONF['detachievement']['tingkat_options']['PE'],
					$CONF['detachievement']['tingkat_options']['PR']),
				'usrAlias'=>$usrAlias,
				'usrName'=>$usrName
			),$data)
		);
		$this->initialize_layout($layoutConf);
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript'] = array_merge($pageConf['jscript_default'],
			array("users/bap/detailView_CR",'datepicker','Swiff.Uploader',
				'Fx.ProgressBar', 'utils/searchDialog','FancyUpload',
				'mooEditable/MooEditable', 'mooEditable/MooEditable.UI.MenuList',
				'mooEditable/MooEditable.Extras',
				));
		
		$pageConf['css'] = array_merge($pageConf['css_default'], 
			array('datepicker','FancyUpload', 'mooEditable/MooEditable', 
				'MooEditable/MooEditable.Extras','MooEditable/MooEditable.SilkTheme'));
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();
	}
	
	function __detailView_CA($bapID){
		$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($usrName);
		
		//load the perbuatan data
		$this->load->model(array('bap_model','perbuatan_model','perbuatan_detil_model',
			'perbuatan_bukti_model','pelapor_model', 'karyawan_model', 'karyawan_history_jabatan_model'));
		$data = array();
		$bapData =  (array) $this->bap_model->select_by_id($bapID)->row();
		$perbData = (array) $this->perbuatan_model->select_by_id($bapData['perb_id'])->row();
		$buktiperbData = $this->perbuatan_bukti_model->select_by_perbuatan($perbData['perb_id']);
		$deperbData = $this->perbuatan_detil_model->select_by_perbuatan($perbData['perb_id']);
		$laporData = (array)$this->pelapor_model->select_by_id($perbData['lapor_id'])->row();
		
		//insert to variable
		$data['bapData'] = $bapData;
		$data['perbData'] = $perbData;
		$data['laporData'] = $laporData;
		foreach($deperbData->result_array() as $res){
			$data['deperbData'][] = $res;
		}
		foreach($buktiperbData->result_array() as $res){
			$data['buktiperbData'][] = $res;
		}
		
		//layout the view
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');

		//layout first
		$this->load->model('kota_model');
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array(
			'view'=>'template/horz_nav', 
			'view_data'=>array(
				'data'=>$this->_translate_nav($CONF['nav']['users'],$usrAlias),
			)
		);
		$layoutConf['right_content'] = array(
			'view'=>"bap/users/detailView_CA",
			'view_data'=>array_merge(array(
				'kota_options'=> $this->kota_model->select_all(array('kota_nama'=>'asc')),
				'jenis_options'=>$CONF['achievement']['jenis_options'],
				'tingkat_options'=>array_merge(
					$CONF['detachievement']['tingkat_options']['PE'],
					$CONF['detachievement']['tingkat_options']['PR']),
				'usrAlias'=>$usrAlias,
				'usrName'=>$usrName
			),$data)
		);
		$this->initialize_layout($layoutConf);
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript'] = array_merge($pageConf['jscript_default'],
			array("users/bap/detailView_SA",'datepicker','Swiff.Uploader',
				'Fx.ProgressBar', 'utils/searchDialog','FancyUpload',
				'mooEditable/MooEditable', 'mooEditable/MooEditable.UI.MenuList',
				'mooEditable/MooEditable.Extras',
				));
		
		$pageConf['css'] = array_merge($pageConf['css_default'], 
			array('datepicker','FancyUpload', 'mooEditable/MooEditable', 
				'MooEditable/MooEditable.Extras','MooEditable/MooEditable.SilkTheme'));
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();
	}
	
	function __detailView_SA($bapID){
		$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($usrName);
		
		//load the perbuatan data
		$this->load->model(array('bap_model','perbuatan_model','perbuatan_detil_model',
			'perbuatan_bukti_model','pelapor_model', 'karyawan_model', 'karyawan_history_jabatan_model'));
		$data = array();
		$bapData =  (array) $this->bap_model->select_by_id($bapID)->row();
		$perbData = (array) $this->perbuatan_model->select_by_id($bapData['perb_id'])->row();
		$buktiperbData = $this->perbuatan_bukti_model->select_by_perbuatan($perbData['perb_id']);
		$deperbData = $this->perbuatan_detil_model->select_by_perbuatan($perbData['perb_id']);
		$laporData = (array)$this->pelapor_model->select_by_id($perbData['lapor_id'])->row();
		
		//insert to variable
		$data['bapData'] = $bapData;
		$data['perbData'] = $perbData;
		$data['laporData'] = $laporData;
		foreach($deperbData->result_array() as $res){
			$data['deperbData'][] = $res;
		}
		foreach($buktiperbData->result_array() as $res){
			$data['buktiperbData'][] = $res;
		}
		
		//layout the view
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');

		//layout first
		$this->load->model('kota_model');
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array(
			'view'=>'template/horz_nav', 
			'view_data'=>array(
				'data'=>$this->_translate_nav($CONF['nav']['users'],$usrAlias),
			)
		);
		$layoutConf['right_content'] = array(
			'view'=>"bap/users/detailView_SA",
			'view_data'=>array_merge(array(
				'kota_options'=> $this->kota_model->select_all(array('kota_nama'=>'asc')),
				'jenis_options'=>$CONF['achievement']['jenis_options'],
				'tingkat_options'=>array_merge(
					$CONF['detachievement']['tingkat_options']['PE'],
					$CONF['detachievement']['tingkat_options']['PR']),
				'usrAlias'=>$usrAlias,
				'usrName'=>$usrName
			),$data)
		);
		$this->initialize_layout($layoutConf);
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript'] = array_merge($pageConf['jscript_default'],
			array("users/bap/detailView_SA",'datepicker','Swiff.Uploader',
				'Fx.ProgressBar', 'utils/searchDialog','FancyUpload',
				'mooEditable/MooEditable', 'mooEditable/MooEditable.UI.MenuList',
				'mooEditable/MooEditable.Extras',
				));
		
		$pageConf['css'] = array_merge($pageConf['css_default'], 
			array('datepicker','FancyUpload', 'mooEditable/MooEditable', 
				'MooEditable/MooEditable.Extras','MooEditable/MooEditable.SilkTheme'));
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();
	}
	
	function _signing($type){
		$this->load->helper(array('url','notification'));
		$this->load->model('bap_model');
		$this->load->model('bap_sign_model');
		$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($usrName);
		
		//sign oleh saksi
		if ($type=='WT'){
			$perb_id = $this->input->post('perb_id');
			$bap_id = "";
			
			$bapSaksi = $this->bap_sign_model->select_by_perbID($perb_id);
			foreach($bapSaksi->result_array() as $row){
				$this->bap_sign_model->sign($row['bap_id'],$usrName,$type);
				$bap_id = $row['bap_id'];
			}
			
			//cek apakah semua saksi telah memberikan ttd, jika ya update status bap-nya
			if ( $this->bap_model->is_signed_by_witnesses($bap_id) ){
				$this->bap_model->update(array('bap_id'=>$bap_id), 
					array('bap_status'=>'SI','bap_status_change'=>date('Y-m-d H:i:s',time())));
				
				//buat notifikasi untuk target user (pelaku)
				createBAPNotification($bap_id, 'SI');
			}
			redirect( site_url(array('users',$usrAlias,'perbuatan')) );
		}
		
		//sign oleh pelaku
		else if ($type=='AC'){
			$bap_id = $this->input->post('bap_id');
			$this->bap_sign_model->sign($bap_id,$usrName,$type);
			
			//cek apakah pelaku telah memberikan ttd, jika ya update status bap-nya
			if ( $this->bap_model->is_signed_by_actor($bap_id) ){
				$this->bap_model->update(array('bap_id'=>$bap_id), 
					array('bap_status'=>'SA',
					'bap_status_change'=>date('Y-m-d H:i:s',time())));
				
				//buat notifikasi untuk target user (pelaku)
				createBAPNotification($bap_id, 'SA');

				//insert penilaian
				$this->load->helper('penilaian');
				insertPointPenilaian($bap_id);
			}
			
			redirect( site_url(array('users',$usrAlias,'bap')) );
		}
	}
	
	function _counter($func){ $this->{"__counter_$func"}(); }
	
	function __counter_save(){
		$this->load->model(array('bap_model','bap_sanggahan_model','bap_sanggahan_bukti_model'));
		
		$inp = $this->input->post('inp');
		$inp = $inp['CO'];
		$res = true;
		
		$res &= $this->bap_sanggahan_model->insert(array_combine(
			array('bap_id','bap_sanggahan_pembuat_kary_id','bap_sanggahan_pembuat_kary_jbt_id',
				'bap_sanggahan_tgl','bap_sanggahan_keterangan'),
			array($inp['bap_id'], $inp['pembuat']['kary_id'], $inp['pembuat']['kary_jbt_id'],
				date('Y-m-d H:i:s',time()), $inp['keterangan'])
		));

		//save the uploaded file to the database
		if (is_array($inp['upload'])){
			$fields =array('bap_bukti_file_size','bap_bukti_file_name', 'bap_bukti_file_type',
				'bap_bukti_content','bap_id');
			
			foreach($inp['upload'] as $ff){
				$path = "system/cache/$ff";
				$value = array();
				$fileSize =  filesize ($path);
				
				$value[] = $fileSize;
				$value[] = addslashes(preg_replace('/(.*)\[/i','', str_replace(']','',$ff)));
				
				$info = @getimagesize($path);
				$value[] = $info['mime'];
				$value[]  = fread( fopen($path, "r"), $fileSize ); 
				$value[] = $inp['bap_id'];
				
				$res &= $this->bap_sanggahan_bukti_model->insert(array_combine($fields, $value));
				
				//delete file
				@unlink($path);
			}
		}
		
		//update bap status
		$this->bap_model->update(
			array('bap_id'=>$inp['bap_id']),
			array('bap_status'=>'CO','bap_status_change'=>date('Y-m-d H:i:s',time()))
		);
		
		//create notifikasi untuk atasan level 2
		$this->load->helper('notification');
		createBAPNotification($inp['bap_id'], 'CO');
		
		//send message
		$this->load->library('session');
		$usrAlias = md5( $this->session->userdata('usrname') );
		echo json_encode(array(
			'error'=>!$res,
			'msg'=>"Proses simpan selesai",
			'url'=>site_url(array('users',$usrAlias,'bap'))));
	}
	
	function __counter_approve(){
		$inp = $this->input->post('inp');
		$inp = $inp['CO'];
		
		$this->load->helper(array('url','notification'));
		$this->load->model(array('bap_sanggahan_model','bap_model'));
		
		$res = $this->bap_sanggahan_model->counterApprove($inp['bap_id'], $inp['pembuat']['kary_id'],	
    		$inp['pembuat']['kary_jbt_id'], $inp['keterangan'], date("Y-m-d H:i:s",time()));
    		
    	if (!$res){
    		echo json_encode( array( 'error'=>!$res, 'msg'=>"Proses simpan gagal, ".$this->bap_sanggahan_model->mysql_innodb_error(mysql_errno())) );
			return;
    	}
    	
    	//update bap status
		$this->bap_model->update(
			array('bap_id'=>$inp['bap_id']),
			array('bap_status'=>'CA','bap_status_change'=>date('Y-m-d H:i:s',time()))
		);
		
		//create notifikasi untuk pelaku dan atasan level 1
		$this->load->helper('notification');
		createBAPNotification($inp['bap_id'], 'CA');
		
		//send message
		$this->load->library('session');
		$usrAlias = md5( $this->session->userdata('usrname') );
		echo json_encode(array(
			'error'=>!$res,
			'msg'=>"Proses simpan selesai",
			'url'=>site_url(array('users',$usrAlias,'bap'))));
	}
	
	function __counter_reject(){
		$inp = $this->input->post('inp');
		$inp = $inp['CO'];
		
		$this->load->helper(array('url','notification'));
		$this->load->model(array('bap_sanggahan_model','bap_model'));
		
		$res = $this->bap_sanggahan_model->counterReject($inp['bap_id'], $inp['pembuat']['kary_id'],	
    		$inp['pembuat']['kary_jbt_id'], $inp['keterangan'], date("Y-m-d H:i:s",time()));
    		
    	if (!$res){
    		echo json_encode( array( 'error'=>!$res, 'msg'=>"Proses simpan gagal, ".$this->bap_sanggahan_model->mysql_innodb_error(mysql_errno())) );
			return;
    	}
    	
    	//update bap status
		$this->bap_model->update(
			array('bap_id'=>$inp['bap_id']),
			array('bap_status'=>'CR','bap_status_change'=>date('Y-m-d H:i:s',time()))
		);
		
		//create notifikasi untuk pelaku dan atasan level1
		$this->load->helper('notification');
		createBAPNotification($inp['bap_id'], 'CR');
		
		//insert penilaian, langsung aktifkan
		$this->load->helper('penilaian');
		insertPointPenilaian($inp['bap_id']);
		
		//send message
		$this->load->library('session');
		$usrAlias = md5( $this->session->userdata('usrname') );
		echo json_encode(array(
			'error'=>!$res,
			'msg'=>"Proses simpan selesai",
			'url'=>site_url(array('users',$usrAlias,'bap'))));
	}
	
	function _loadImage($image){
		$this->load->model('perbuatan_bukti_model');
		$img = (array)$this->perbuatan_bukti_model->select_by_id($image)->row();
		header("Content-type: ".$img['perb_dok_file_type']);
		print $img['perb_dok_content'];
	}
}
/* End of file users/bap.php */
/* Location: ./system/application/controllers/users/bap.php */